package cn.shyouchi.wuguanjia.android.ui.event.checknum;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PreviewCheckNumActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_check_num)
    RecyclerView mRvCheckNum;
    @BindView(R.id.btn_del)
    Button mBtnDel;
    @BindView(R.id.btn_edit)
    Button mBtnEdit;
    CheckNumEntity.ResultMessageBean entity;
    String projectId;

    PreviewCheckPointAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int initContentView() {
        return R.layout.activity_preview_check_num;
    }

    @Override
    public void initInjector() {
        entity = getIntent().getParcelableExtra("checknum");
        projectId = getIntent().getStringExtra("projectId");

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());
        mAdapter = new PreviewCheckPointAdapter(entity.getInfo());
        mRvCheckNum.setAdapter(mAdapter);
        mRvCheckNum.setLayoutManager(new LinearLayoutManager(mContext));

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @OnClick({R.id.btn_del, R.id.btn_edit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_del:
                EventApi mEventApi = new EventApi(mContext);
                mEventApi.delCheckNum(String.valueOf(entity.getId()))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                            @Override
                            public void onNext(BaseRequestResult result) {
                                EventBus.getDefault().post(new DataChangeEvent());
                                finish();
                            }
                        });
                break;
            case R.id.btn_edit:
                Intent intent = new Intent(mContext, AddEventCheckNumActivity.class);
                intent.putExtra("checknum", entity);
                intent.putExtra("projectId", projectId);
                startActivity(intent);
                finish();
                break;
        }
    }
}
